import {
  DataSource,
  EntitySubscriberInterface,
  EventSubscriber,
  InsertEvent,
} from 'typeorm';
import { UserEntity } from './user.mysql.entity';
import { BcryptService } from "@/shared/common/bcryptjs.service"

@EventSubscriber()
export class UserSubscriber implements EntitySubscriberInterface<UserEntity> {
  constructor(dataSource: DataSource) {
    dataSource.subscribers.push(this);
  }

  listenTo() {
    return UserEntity;
  }

  async beforeInsert(event: InsertEvent<UserEntity>) {
    console.log(`BEFORE USER INSERTED: `, event.entity);
    event.entity.password = await BcryptService.hash(event.entity.password)
  }
}